// pages/travel/travel.js
var app = getApp();
var schoolName = "";
var locationName = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: true,
    show:false,
    imgData:[],
    checked : false,
    winWth:"",
    dataList:[],
    commentVal:"",
    commentShow:"true",
    commentList:"",
    isLoading:"true"
  },
 //获取屏幕尺寸
  getWinWth(){
     var winWth=wx.getSystemInfoSync().screenWidth;    
    this.setData({
      winWth:winWth
    })
  },
  //浮动发布按钮
  floatBtn(){
    if(app.globalData.userInfoName){
      wx.showActionSheet({
        itemList:["快递","外卖","其他"],
        success:res=>{
          wx.navigateTo({
            url: '/pages/sendmsg/sendmsg?tapIdx='+res.tapIndex
          })
        }
      })
      this.setData({ show: true });
    }
    else
    {
        wx.navigateTo({
          url: '/pages/login/login',
        })
    }
  },
//获取页面数据
  getpageData(num=0,page=7)//skip,limit
  {
    console.log(schoolName)
    console.log(locationName)
    wx.cloud.callFunction({
      name:"jiedan_info_get",
      data:{
        num:num,
        page:page,
        schoolName,
        locationName
      }, 
      success:res=>{
       var newDataList=[...this.data.dataList,...res.result]
       console.log(newDataList)
        if(res.result.length==0||res.result.length<7){
          this.setData({
            isLoading:false
          })
        }
        this.setData({
          dataList:newDataList
        })
      }
  })
},

  //接单
 async click_zan(res){
     if(app.globalData.userInfo)
    {
    var id = res.currentTarget.dataset.id
    var res1 = await wx.cloud.callFunction({
        name: 'jiedan_check',
        data: {
          id,
          schoolName,
          locationName
      },
        success:res=>{
          console.log(res)
          if(res.result ){
            console.log("已认证")     
            this.setData({
              checked : true
            })
             console.log(this.data.checked)
          }
          else{
            console.log("未认证")
            this.setData({
              checked : false
            })
          }

        },
      })
      wx.showModal({
        title:"请确认您的操作",
        success:r=>{
          setTimeout(function() {
            //要延时执行的代码
          }, 1000) //延迟时间
          if(r.confirm)
          {
            wx.showLoading({
              mask:true
            })
                if(this.data.checked)
                {
                  var id = res.currentTarget.dataset.id
                  var idx = res.currentTarget.dataset.idx
                 wx.cloud.callFunction({
                    name: 'jiedan_jiedan',
                    data: {
                      id:id,
                    },
                    success:res=>{
                      var _dataList = this.data.dataList;
                      _dataList[idx].isZan = !_dataList[idx].isZan
                      var isZan  = this.data.dataList[idx].isZan;
                      var zanSize = this.data.dataList[idx].zanSize;
                      if(isZan){
                        zanSize--;
                        _dataList[idx].zanSize = zanSize;
                      }
                      else
                      {
                        zanSize++;
                        _dataList[idx].zanSize = zanSize;
                      }
                      this.setData({
                        dataList:_dataList
                      })
                      
                      wx.hideLoading()
                    },
                    fail: console.error
                  })
                }
              else{
                wx.showToast({
                  icon:"error",
                  title: '认证或校区错误',
                })
              }
          }
        }
      })
     
     }
     else{
       wx.navigateTo({
         url: '/pages/login/login',
       })
     }
   
  },


//删除发布的帖子
    clickDel(e){
      var id = e.currentTarget.dataset.id
      var index = e.currentTarget.dataset.index
      wx.showModal({
        title:"是否确认删除",
        success:res=>{
          if(res.confirm)
          {
            wx.showLoading({
              mask:true
            })
            wx.cloud.callFunction({
              name: "jiedan_info_del",
              data:{
                id
              },
              success:res=>{
                var _dataList = this.data.dataList
                _dataList.splice(index,1)
                this.setData({
                  dataList:_dataList
                })
                wx.hideLoading()
              }
            })
          }
        }
      })
    },
 
  //查看大图
  checkImage(res){
    // console.log(res)
    wx.previewImage({
      urls:res.currentTarget.dataset.urls,
      current: res.currentTarget.dataset.current
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      wx.showLoading({
        title: '数据加载中',
        mask:true
      },1000)
        schoolName = wx.getStorageSync("schoolName");
      locationName =  wx.getStorageSync("locationName");
        this.getWinWth()
        this.getpageData();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setData({
      loading: false,
    });
    wx.hideLoading()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var num=this.data.dataList.length;
    this.getpageData(num);

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})